function data = GET_TransverseFieldZ_StepResponse(obj, varargin)
%GET_TRANSVERSEFIELDZ_STEPRESPONSE Summary of this function goes here
%   Detailed explanation goes here
    p=inputParser;
    p.addParameter('stepSize', 0.1, @isnumeric);
    p.addParameter('isPlot', true, @islogical);
    p.parse(varargin{:});
    %%
    dv = p.Results.stepSize;
    volt = obj.controller.getCurrentControlVoltZ;
    obj.controller.unLockMagnetometerTransverseFieldZ;

    %%
    t = timer('ExecutionMode', 'fixedRate', 'startDelay', 1.0, 'Period', 3.0, ...
              'TimerFcn', @(~,thisEvent) toggle_voltage_Z(obj, volt, dv));
    start(t);

    %%
    zi = obj.controller.ziMaster();
    da = zi.daqModule();
    
    da.setColNum(2048);
    da.setTriggerSource('3', 'AuxIn1');
    da.setTriggerEdge(2);
    da.setTriggerLevel( -(volt+0.5*dv) );
    da.setTriggerHysteresis(0.000);
    da.setTriggerHoldoff(0.0);
    da.setTriggerDelay(-0.100);

    da.reset();
    da.subscribeChannel('3', 'sample.X');
    da.subscribeChannel('3', 'sample.AuxIn1');
    data= da.run();
    da.unsubscribeChannel();
    
    %%
    stop(t); delete(t);
    obj.controller.lockMagnetometerTransverseFieldZ;
    
    if p.Results.isPlot
        response = data.getNodeData('3', 'sample.x');
        trig = data.getNodeData('3', 'sample.AuxIn1');
        figure('Name', sprintf('Step Response of TransverseField Z, dv = %f', dv));
        yyaxis left;
        plot(trig.time, trig.value, 'd-');
        yyaxis right;
        plot(response.time, response.value, 'o-'); grid on;
        xlim(minmax(response.time.'));
    end
end

function toggle_voltage_Z(obj, volt, dv)
    obj.controller.setCurrentControlVoltZ(volt+dv); pause(1);
    obj.controller.setCurrentControlVoltZ(volt); pause(1);
end